package net.caixiaomi.info.ui.football;

import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.View;

import net.caixiaomi.info.R;
import net.caixiaomi.info.app.CommonApp;
import net.caixiaomi.info.base.quickadapter.BaseViewHolder;
import net.caixiaomi.info.base.quickadapter.entity.MultiItemEntity;
import net.caixiaomi.info.model.ElementCellModel;
import net.caixiaomi.info.model.MatchPlaysList;
import net.caixiaomi.info.model.PlayFootballItem;
import net.caixiaomi.info.util.Constants;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * 混合投注
 */

public class BlendProxy extends ItemViewProxy<MultiItemEntity, BaseViewHolder> {
    private FootballAdapter mAdapter;

    public BlendProxy(FootballAdapter adapter) {
        super(adapter);
        mAdapter = adapter;
    }

    @Override
    public void bindView(BaseViewHolder helper, MultiItemEntity item) {
        try {
            PlayFootballItem model = (PlayFootballItem) item;
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
            String time = sdf.format(new Date(Long.parseLong(model.getBetEndTime()) * 1000));
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(model.getLeagueAddr()).append("\n").append(model.getChangci()).append("\n").append(CommonApp.getInstance().getString(R.string.C_END_TIME)).append(time);
            helper.setText(R.id.text, stringBuilder);
            helper.addOnClickListener(R.id.content);
            stringBuilder.setLength(0);
            if (!TextUtils.isEmpty(model.getHomeTeamRank())) {
                stringBuilder.append("[").append(model.getHomeTeamRank()).append("]");
            }
            stringBuilder.append(model.getHomeTeamAbbr());
            helper.setText(R.id.host, stringBuilder);

            stringBuilder.setLength(0);
            if (!TextUtils.isEmpty(model.getVisitingTeamRank())) {
                stringBuilder.append("[").append(model.getVisitingTeamRank()).append("]");
            }
            stringBuilder.append(model.getVisitingTeamAbbr());
            helper.setText(R.id.guest, stringBuilder);

            stringBuilder.setLength(0);

            MatchPlaysList matchPlaysList1 = model.getMatchPlays().get(0);//让球胜平负
            if (TextUtils.equals(matchPlaysList1.getShow(), "1")) {
                helper.setGone(R.id.view2, true);
                helper.setGone(R.id.btn_not_open2, false);
                ElementCellModel homeCell1 = matchPlaysList1.getHomeCell();
                homeCell1.setPlayType(matchPlaysList1.getPlayType());
                stringBuilder.append(homeCell1.getCellName()).append(homeCell1.getCellOdds());
                helper.setText(R.id.victory2, stringBuilder);
                helper.setTextColor(R.id.victory2, ContextCompat.getColor(CommonApp.getInstance(), homeCell1.isSelected() ? R.color.white : R.color.third_text));
                helper.getView(R.id.victory2).setSelected(homeCell1.isSelected());
                helper.setTag(R.id.victory2, R.id.item_model, model);
                helper.setTag(R.id.victory2, R.id.item_cell, homeCell1);
                helper.setOnClickListener(R.id.victory2, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ElementCellModel tag = (ElementCellModel) v.getTag(R.id.item_cell);
                        PlayFootballItem tagModel = (PlayFootballItem) v.getTag(R.id.item_model);
                        if (mAdapter.getListener() != null) {
                            if (mAdapter.getListener().canChange(tagModel)) {
                                tag.setSelected(!tag.isSelected());
                                if (tagModel.getSelectCell() == null) {
                                    tagModel.setSelectCell(new ArrayList<ElementCellModel>());
                                }
                                if (tag.isSelected()) {
                                    tagModel.getSelectCell().add(tag);
                                } else {
                                    tagModel.getSelectCell().remove(tag);
                                }
                                mAdapter.getListener().onItemChangeListener(tagModel);
                            }
                        }
                    }
                });
                stringBuilder.setLength(0);

                ElementCellModel flatCell1 = matchPlaysList1.getFlatCell();
                flatCell1.setPlayType(matchPlaysList1.getPlayType());
                stringBuilder.append(flatCell1.getCellName()).append(flatCell1.getCellOdds());
                helper.setText(R.id.tie2, stringBuilder);
                helper.setTextColor(R.id.tie2, ContextCompat.getColor(CommonApp.getInstance(), flatCell1.isSelected() ? R.color.white : R.color.third_text));
                helper.getView(R.id.tie2).setSelected(flatCell1.isSelected());
                helper.setTag(R.id.tie2, R.id.item_model, model);
                helper.setTag(R.id.tie2, R.id.item_cell, flatCell1);
                helper.setOnClickListener(R.id.tie2, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ElementCellModel tag = (ElementCellModel) v.getTag(R.id.item_cell);
                        PlayFootballItem tagModel = (PlayFootballItem) v.getTag(R.id.item_model);
                        if (mAdapter.getListener() != null) {
                            if (mAdapter.getListener().canChange(tagModel)) {
                                tag.setSelected(!tag.isSelected());
                                if (tagModel.getSelectCell() == null) {
                                    tagModel.setSelectCell(new ArrayList<ElementCellModel>());
                                }
                                if (tag.isSelected()) {
                                    tagModel.getSelectCell().add(tag);
                                } else {
                                    tagModel.getSelectCell().remove(tag);
                                }
                                mAdapter.getListener().onItemChangeListener(tagModel);
                            }
                        }
                    }
                });
                stringBuilder.setLength(0);

                ElementCellModel visitingCell1 = matchPlaysList1.getVisitingCell();
                visitingCell1.setPlayType(matchPlaysList1.getPlayType());
                stringBuilder.append(visitingCell1.getCellName()).append(visitingCell1.getCellOdds());
                helper.setText(R.id.guest2, stringBuilder);
                helper.setTextColor(R.id.guest2, ContextCompat.getColor(CommonApp.getInstance(), visitingCell1.isSelected() ? R.color.white : R.color.third_text));
                helper.getView(R.id.guest2).setSelected(visitingCell1.isSelected());
                helper.setTag(R.id.guest2, R.id.item_model, model);
                helper.setTag(R.id.guest2, R.id.item_cell, visitingCell1);
                helper.setOnClickListener(R.id.guest2, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ElementCellModel tag = (ElementCellModel) v.getTag(R.id.item_cell);
                        PlayFootballItem tagModel = (PlayFootballItem) v.getTag(R.id.item_model);
                        if (mAdapter.getListener() != null) {
                            if (mAdapter.getListener().canChange(tagModel)) {
                                tag.setSelected(!tag.isSelected());
                                if (tagModel.getSelectCell() == null) {
                                    tagModel.setSelectCell(new ArrayList<ElementCellModel>());
                                }
                                if (tag.isSelected()) {
                                    tagModel.getSelectCell().add(tag);
                                } else {
                                    tagModel.getSelectCell().remove(tag);
                                }
                                mAdapter.getListener().onItemChangeListener(tagModel);
                            }
                        }
                    }
                });

                boolean single = TextUtils.equals(matchPlaysList1.getSingle(), "1");
                helper.setBackgroundRes(R.id.view2, single ? R.drawable.bg_white_orange_stroke : R.color.white);
                helper.getView(R.id.view2).setPadding(single ? (int) CommonApp.getInstance().getResources().getDimension(R.dimen.divider) : 0, single ? (int) CommonApp.getInstance().getResources().getDimension(R.dimen.divider) : 0, single ? (int) CommonApp.getInstance().getResources().getDimension(R.dimen.divider) : 0, single ? (int) CommonApp.getInstance().getResources().getDimension(R.dimen.divider) : 0);

                int fixedOdds = Integer.valueOf(matchPlaysList1.getFixedOdds());
                helper.setText(R.id.tip, matchPlaysList1.getFixedOdds());
                helper.setBackgroundColor(R.id.tip, Color.parseColor(fixedOdds > 0 ? "#F2AA4E" : "#8AC271"));

            } else {
                helper.setGone(R.id.view2, false);
                helper.setGone(R.id.btn_not_open2, true);
            }

            stringBuilder.setLength(0);
            MatchPlaysList matchPlaysList2 = model.getMatchPlays().get(1);//胜平负
            if (TextUtils.equals(matchPlaysList2.getShow(), "1")) {
                helper.setGone(R.id.view1, true);
                helper.setGone(R.id.btn_not_open1, false);
                ElementCellModel homeCell2 = matchPlaysList2.getHomeCell();
                homeCell2.setPlayType(matchPlaysList2.getPlayType());
                stringBuilder.append(homeCell2.getCellName()).append(homeCell2.getCellOdds());
                helper.setText(R.id.victory1, stringBuilder);
                helper.setTextColor(R.id.victory1, ContextCompat.getColor(CommonApp.getInstance(), homeCell2.isSelected() ? R.color.white : R.color.third_text));
                helper.getView(R.id.victory1).setSelected(homeCell2.isSelected());
                helper.setTag(R.id.victory1, R.id.item_model, model);
                helper.setTag(R.id.victory1, R.id.item_cell, homeCell2);
                helper.setOnClickListener(R.id.victory1, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ElementCellModel tag = (ElementCellModel) v.getTag(R.id.item_cell);
                        PlayFootballItem tagModel = (PlayFootballItem) v.getTag(R.id.item_model);
                        if (mAdapter.getListener() != null) {
                            if (mAdapter.getListener().canChange(tagModel)) {
                                tag.setSelected(!tag.isSelected());
                                if (tagModel.getSelectCell() == null) {
                                    tagModel.setSelectCell(new ArrayList<ElementCellModel>());
                                }
                                if (tag.isSelected()) {
                                    tagModel.getSelectCell().add(tag);
                                } else {
                                    tagModel.getSelectCell().remove(tag);
                                }
                                mAdapter.getListener().onItemChangeListener(tagModel);
                            }
                        }
                    }
                });
                stringBuilder.setLength(0);

                ElementCellModel flatCell2 = matchPlaysList2.getFlatCell();
                flatCell2.setPlayType(matchPlaysList2.getPlayType());
                stringBuilder.append(flatCell2.getCellName()).append(flatCell2.getCellOdds());
                helper.setText(R.id.tie1, stringBuilder);
                helper.setTextColor(R.id.tie1, ContextCompat.getColor(CommonApp.getInstance(), flatCell2.isSelected() ? R.color.white : R.color.third_text));
                helper.getView(R.id.tie1).setSelected(flatCell2.isSelected());
                helper.setTag(R.id.tie1, R.id.item_model, model);
                helper.setTag(R.id.tie1, R.id.item_cell, flatCell2);
                helper.setOnClickListener(R.id.tie1, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ElementCellModel tag = (ElementCellModel) v.getTag(R.id.item_cell);
                        PlayFootballItem tagModel = (PlayFootballItem) v.getTag(R.id.item_model);
                        if (mAdapter.getListener() != null) {
                            if (mAdapter.getListener().canChange(tagModel)) {
                                tag.setSelected(!tag.isSelected());
                                if (tagModel.getSelectCell() == null) {
                                    tagModel.setSelectCell(new ArrayList<ElementCellModel>());
                                }
                                if (tag.isSelected()) {
                                    tagModel.getSelectCell().add(tag);
                                } else {
                                    tagModel.getSelectCell().remove(tag);
                                }
                                mAdapter.getListener().onItemChangeListener(tagModel);
                            }
                        }
                    }
                });
                stringBuilder.setLength(0);

                ElementCellModel visitingCell2 = matchPlaysList2.getVisitingCell();
                visitingCell2.setPlayType(matchPlaysList2.getPlayType());
                stringBuilder.append(visitingCell2.getCellName()).append(visitingCell2.getCellOdds());
                helper.setText(R.id.guest1, stringBuilder);
                helper.setTextColor(R.id.guest1, ContextCompat.getColor(CommonApp.getInstance(), visitingCell2.isSelected() ? R.color.white : R.color.third_text));
                helper.getView(R.id.guest1).setSelected(visitingCell2.isSelected());
                helper.setTag(R.id.guest1, R.id.item_model, model);
                helper.setTag(R.id.guest1, R.id.item_cell, visitingCell2);
                helper.setOnClickListener(R.id.guest1, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ElementCellModel tag = (ElementCellModel) v.getTag(R.id.item_cell);
                        PlayFootballItem tagModel = (PlayFootballItem) v.getTag(R.id.item_model);
                        if (mAdapter.getListener() != null) {
                            if (mAdapter.getListener().canChange(tagModel)) {
                                tag.setSelected(!tag.isSelected());
                                if (tagModel.getSelectCell() == null) {
                                    tagModel.setSelectCell(new ArrayList<ElementCellModel>());
                                }
                                if (tag.isSelected()) {
                                    tagModel.getSelectCell().add(tag);
                                } else {
                                    tagModel.getSelectCell().remove(tag);
                                }
                                mAdapter.getListener().onItemChangeListener(tagModel);
                            }
                        }
                    }
                });
                boolean single = TextUtils.equals(matchPlaysList2.getSingle(), "1");

                helper.setBackgroundRes(R.id.view1, single ? R.drawable.bg_white_orange_stroke : R.color.white);
                helper.getView(R.id.view1).setPadding(single ? (int) CommonApp.getInstance().getResources().getDimension(R.dimen.divider) : 0, single ? (int) CommonApp.getInstance().getResources().getDimension(R.dimen.divider) : 0, single ? (int) CommonApp.getInstance().getResources().getDimension(R.dimen.divider) : 0, single ? (int) CommonApp.getInstance().getResources().getDimension(R.dimen.divider) : 0);
            } else {
                helper.setGone(R.id.view1, false);
                helper.setGone(R.id.btn_not_open1, true);
            }

            helper.setVisible(R.id.action, TextUtils.equals(matchPlaysList2.getSingle(), "1") ||  TextUtils.equals(matchPlaysList1.getSingle(), "1"));

            if (model.getSelectCell() != null && !model.getSelectCell().isEmpty()) {
                String text = CommonApp.getInstance().getString(R.string.C_FOOTBALL_HAS_SELECT_NUM, model.getSelectCell().size());
                SpannableString spannableString = new SpannableString(text);
                String size = String.valueOf(model.getSelectCell().size());
                spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), R.color.orange_primary)), text.length() - size.length() - 1, text.length() - 1, SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
                helper.setText(R.id.btn_more, spannableString);
            } else {
                helper.setText(R.id.btn_more, R.string.C_FOOTBALL_MORE_PLAY);
            }
            helper.addOnClickListener(R.id.btn_more);

            helper.addOnClickListener(R.id.btn_float);
            helper.setGone(R.id.btn_float, TextUtils.equals("1", model.isShutDown()));


            if (helper.getAdapterPosition() + 1 <= mAdapter.getData().size() - 1) {
                MultiItemEntity nextItem = mAdapter.getData().get(helper.getAdapterPosition() + 1);
                helper.setGone(R.id.line, nextItem.getItemType() == item.getItemType());
            } else {
                helper.setGone(R.id.line, false);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void addItemType() {
        mAdapter.addItemType(Constants.ITEM_VIEW_HEADER, R.layout.header_football);
        mAdapter.addItemType(Constants.ITEM_VIEW_ITEM, R.layout.item_blend_proxy);
    }
}
